home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Graphics 2D / Palette and GWorld / Palette and GWorld.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-06  |  9.8 KB  |  331 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        Palette and GWorld.c
  3.  
  4.     Contains:    This app copies from two offscreen GWorlds into the left and right halves of
  5.                 the window. The contents of the GWorlds are vertical lines that show all the
  6.                 entries in the color tables associated with the GWorlds. The GWorlds were created
  7.                 as described below. 
  8.  
  9.                 One commonly asked question is how to use a palette when drawing into a GWorld.
  10.                 The trick is understanding that while setting a palette to a GWorld is permitted,
  11.                 doing so does not change the GWorld’s color table. 
  12.  
  13.                 The solution is to make a palette from the color table ( or the color table from
  14.                 a palette) and to use that color table to create or update the GWorld. After then
  15.                 doing a SetGWorld you can either draw with Index2Color and RGBForeColor, or you
  16.                  can set the palette to the GWorld and draw with PmForeColor. These techniques are
  17.                 shown in the procedures createRGBForeColorImage and createPmForeColorImage in
  18.                 Palette and GWorld.c. 
  19.  
  20.  
  21.     Written by:     
  22.  
  23.     Copyright:    Copyright © 1999 by Apple Computer, Inc., All Rights Reserved.
  24.  
  25.                 You may incorporate this Apple sample source code into your program(s) without
  26.                 restriction. This Apple sample source code has been provided "AS IS" and the
  27.                 responsibility for its operation is yours. You are not permitted to redistribute
  28.                 this Apple sample source code as "Apple sample source code" after having made
  29.                 changes. If you're going to re-distribute the source, we require that you make
  30.                 it clear in the source that the code was descended from Apple sample source
  31.                 code, but that you've made changes.
  32.  
  33.     Change History (most recent first):
  34.                 08/2000        JM                Carbonized, non-Carbon code is commented out
  35.                                             for demonstration purposes.
  36.                 7/13/1999    KG                Updated for Metrowerks Codewarror Pro 2.1
  37.                 
  38.  
  39. *//*  Includes  */
  40. #include "CarbonPrefix.h"
  41. #include <QDOffscreen.h>
  42. #include <Palettes.h>
  43. #include <Memory.h>
  44. #include <Fonts.h>
  45. #include <Windows.h>
  46. #include <Menus.h>
  47. #include <TextEdit.h>
  48. #include <Dialogs.h>
  49. #include <Events.h>
  50.  
  51. /*  Defines  */
  52. static     BitMap    globalBitMap;                    //declared here to use in a #define (for carbonization)
  53. #define    TOTALCOLORS    256
  54. #define    WWIDTH        (TOTALCOLORS * 2)
  55. #define    WHALFWIDTH    TOTALCOLORS
  56. #define    WHEIGHT        TOTALCOLORS
  57. //#define WLEFT        (((qd.screenBits.bounds.right - qd.screenBits.bounds.left) - WWIDTH) / 2)
  58. //#define WTOP        (((qd.screenBits.bounds.bottom - qd.screenBits.bounds.top) - WHEIGHT) / 2)
  59. #define WLEFT        (((GetQDGlobalsScreenBits(&globalBitMap)->bounds.right - GetQDGlobalsScreenBits(&globalBitMap)->bounds.left) - WWIDTH) / 2)
  60. #define WTOP        (((GetQDGlobalsScreenBits(&globalBitMap)->bounds.bottom - GetQDGlobalsScreenBits(&globalBitMap)->bounds.top) - WHEIGHT) / 2)
  61.  
  62. /*  Global Variable Definitions  */
  63. /*#ifdef    powerc
  64.     QDGlobals             qd;
  65. #endif*/
  66. WindowPtr            gWindow;
  67. PaletteHandle        gPalette;
  68. GWorldPtr            gRGBGWorld;
  69. GWorldPtr            gPmGWorld;
  70. PixMapHandle        gRGBPixMap;
  71. PixMapHandle        gPmPixMap;
  72. CTabHandle            gClut;
  73. Rect                gRGBRect = { 0, 0, WHEIGHT, WHALFWIDTH };
  74. Rect                gPmRect  = { 0, WHALFWIDTH, WHEIGHT, WWIDTH };
  75. short                gMode    = srcOr;
  76. short                gText    = kFontIDTimes;
  77. short                gSize    = 24;
  78.  
  79. /*  Procedure Prototypes  */
  80. void initMac( void );
  81. void makePalette( void );
  82. void createWindow( void );
  83. void createOffscreens( void );
  84. void createRGBForeColorImage( void );
  85. void createPmForeColorImage( void );
  86. void drawWindow( void );
  87. void doEventLoop( void );
  88.  
  89.  
  90. /*             */
  91. /*  Main loop. */
  92. /*             */
  93.  
  94. void main( void )
  95. {
  96.     initMac();
  97.     
  98.     makePalette();
  99.     createWindow();    
  100.     createOffscreens();
  101.     createRGBForeColorImage();
  102.     createPmForeColorImage();
  103.  
  104.     doEventLoop();
  105. }
  106.  
  107.  
  108. /*                      */
  109. /*  Mac initialization. */
  110. /*                      */
  111.  
  112. void initMac( void )
  113. {
  114.     /*MaxApplZone();
  115.     MoreMasters();
  116.     MoreMasters();
  117.     InitGraf( &qd.thePort );
  118.     InitFonts();
  119.     InitWindows();
  120.     InitMenus();
  121.     TEInit();
  122.     InitDialogs( (long)nil );*/
  123.     MoreMasterPointers(2);
  124.     InitCursor();
  125.     FlushEvents( 0, everyEvent );
  126. }
  127.  
  128.  
  129. /*                                    */
  130. /*  Create the palette from the clut. */
  131. /*                                    */
  132.  
  133. void makePalette( void )
  134. {    
  135.     short    plttID = 128;
  136.     
  137.     /*  Get the color table from the .rsrc file. */
  138.     gClut = GetCTable( plttID );
  139.     
  140.     /*  Create the palette from the color table.                                                  */
  141.     /*  pmExplicit says we want the colors in the same order as the color table.                  */
  142.     /*  pmTolerant with a tolerance of 0 says we want exactly the same colors as the color table. */
  143.     gPalette = NewPalette( TOTALCOLORS, gClut, pmExplicit + pmTolerant, 0 );
  144. }
  145.  
  146.  
  147. /*                                                                     */
  148. /*  Create the window to display the images and set the palette to it. */
  149. /*                                                                     */
  150.  
  151. void createWindow( void )
  152. {
  153.     Rect    bounds;
  154.     
  155.     SetRect( &bounds, WLEFT, WTOP, WLEFT + WWIDTH, WTOP + WHEIGHT );
  156.     
  157.     gWindow = NewCWindow( 0L, &bounds, "\pPalette&GWorld", true, documentProc,
  158.                             (WindowPtr)-1L, true, 0L );                        
  159.     //SetPort( gWindow );
  160.     SetPortWindowPort( gWindow );
  161.     
  162.     /*  Attach the palette to the window. */
  163.     NSetPalette( gWindow, gPalette, pmAllUpdates );
  164. }
  165.  
  166.  
  167. /*                                                           */
  168. /*  Create the GWorlds based on the clut to hold the images. */
  169. /*                                                           */
  170.  
  171. void createOffscreens( void )
  172. {
  173.     GDHandle    screensDevice;
  174.  
  175.     short        depth = 8;
  176.  
  177.     /*  Set the color table's ctSeed equal to that of the main screen's GDevice. */ 
  178.     /*  This tells QuickDraw not to do color mapping.                            */ 
  179.     /*  Color mapping is not needed because the palette matches the color table. */ 
  180.     screensDevice = GetMainDevice();
  181.     if (screensDevice != nil)
  182.         (**gClut).ctSeed = (**(**(**screensDevice).gdPMap).pmTable).ctSeed;
  183.         
  184.     /*  Create the RGBForeColor offscreen world using the colortable. */
  185.     NewGWorld( &gRGBGWorld, depth, &gRGBRect, gClut, nil, 0 );
  186.     gRGBPixMap = GetGWorldPixMap( gRGBGWorld );    
  187.  
  188.     /*  Create the PmForeColor offscreen world using the colortable. */
  189.     NewGWorld( &gPmGWorld, depth, &gPmRect, gClut, nil, 0 );
  190.     gPmPixMap = GetGWorldPixMap( gPmGWorld );    
  191. }
  192.  
  193.  
  194. /*                                                              */
  195. /*  Create the GWorld image using Index2Color and RGBForeColor. */
  196. /*                                                              */
  197.  
  198. void createRGBForeColorImage( void )
  199. {
  200.     int            i;
  201.     CGrafPtr    currentPort;
  202.     GDHandle    currentDevice;
  203.     RGBColor    color;
  204.     FontInfo    info;
  205.     Str255        string = "\pRGBForeColor";
  206.     
  207.     GetGWorld( ¤tPort, ¤tDevice );
  208.     
  209.     SetGWorld( gRGBGWorld, nil );
  210.     PenSize ( 1, 1 );
  211.     LockPixels( gRGBPixMap );
  212.     
  213.     /*  Show all the colors in the background using Index2Color and RGBForeColor. */
  214.     for (i = 0; i < TOTALCOLORS; i++){
  215.         Index2Color( i, &color );
  216.         RGBForeColor( &color );
  217.         MoveTo( i, 0 );
  218.         Line( 0, WHEIGHT );
  219.     }
  220.     
  221.     /*  Draw label; RGBForeColor should now be black. */
  222.     TextMode( gMode );
  223.     TextFont( gText );
  224.     TextSize( gSize );        
  225.     GetFontInfo( &info );
  226.     MoveTo( (WHALFWIDTH - StringWidth(string))/2, WHEIGHT/2 - info.ascent/3 );
  227.     DrawString( string );
  228.     
  229.     UnlockPixels( gRGBPixMap );
  230.     SetGWorld( currentPort, currentDevice );
  231. }
  232.  
  233.  
  234. /*                                                                                      */
  235. /*  Create the GWorld image by setting the palette to the GWorld and using PmForeColor. */
  236. /*                                                                                      */
  237.  
  238. void createPmForeColorImage( void )
  239. {
  240.     int            i;
  241.     CGrafPtr    currentPort;
  242.     GDHandle    currentDevice;
  243.     FontInfo    info;
  244.     Str255        string = "\pPmForeColor";
  245.     
  246.     GetGWorld( ¤tPort, ¤tDevice );
  247.     
  248.     SetGWorld( gPmGWorld, nil );
  249.     NSetPalette( (WindowPtr)gPmGWorld, gPalette, pmNoUpdates );
  250.     PenSize ( 1, 1 );
  251.     LockPixels( gPmPixMap );
  252.     
  253.     /*  Show all the colors in the background using PmForeColor. */
  254.     for (i = 0; i < TOTALCOLORS; i++){
  255.         PmForeColor( i );
  256.         MoveTo( (i + WHALFWIDTH), 0 );
  257.         Line( 0, WHEIGHT );
  258.     }
  259.                     
  260.     /*  Draw label; PmForeColor should now be black. */
  261.     TextMode( gMode );
  262.     TextFont( gText );
  263.     TextSize( gSize );        
  264.     GetFontInfo( &info );
  265.     MoveTo( ((WHALFWIDTH - StringWidth(string))/2) + WHALFWIDTH, WHEIGHT/2 - info.ascent/3 );
  266.     DrawString( string );
  267.     
  268.     UnlockPixels( gPmPixMap );
  269.     SetGWorld( currentPort, currentDevice );
  270. }
  271.  
  272.  
  273. /*                                      */
  274. /*  CopyBits both images to the screen. */
  275. /*                                      */
  276.  
  277. void drawWindow( void )
  278. {    
  279.     /*CopyBits( (BitMap*)*gRGBPixMap, &gWindow->portBits, &(**gRGBPixMap).bounds,
  280.                 &gRGBRect, srcCopy, 0l );
  281.                 
  282.     CopyBits( (BitMap*)*gPmPixMap, &gWindow->portBits, &(**gPmPixMap).bounds,
  283.                 &gPmRect, srcCopy, 0l );*/
  284.     CopyBits( (BitMap*)*gRGBPixMap, GetPortBitMapForCopyBits(GetWindowPort(gWindow)), &(**gRGBPixMap).bounds,
  285.                 &gRGBRect, srcCopy, 0l );
  286.                 
  287.     CopyBits( (BitMap*)*gPmPixMap, GetPortBitMapForCopyBits(GetWindowPort(gWindow)), &(**gPmPixMap).bounds,
  288.                 &gPmRect, srcCopy, 0l );
  289. }
  290.  
  291.  
  292. /*              */
  293. /*  Event loop. */
  294. /*              */
  295.  
  296. void doEventLoop( void )
  297. {
  298.     EventRecord event;
  299.     WindowPtr   window;
  300.     short       clickArea;
  301.     Rect        screenRect;
  302.  
  303.     for (;;){
  304.         if (WaitNextEvent( everyEvent, &event, 0, nil )){
  305.             if (event.what == mouseDown){
  306.                 clickArea = FindWindow( event.where, &window );
  307.                 
  308.                 if (clickArea == inDrag){
  309.                     //screenRect = (**GetGrayRgn()).rgnBBox;
  310.                     GetRegionBounds(GetGrayRgn(), &screenRect);
  311.                     DragWindow( window, event.where, &screenRect );
  312.                 }
  313.                 else if (clickArea == inContent){
  314.                     if (window != FrontWindow())
  315.                         SelectWindow( window );
  316.                 }
  317.                 else if (clickArea == inGoAway)
  318.                     if (TrackGoAway( window, event.where ))
  319.                         return;
  320.             }
  321.             else if (event.what == updateEvt){
  322.                 window = (WindowPtr)event.message;    
  323.                 //SetPort( window );    
  324.                 SetPortWindowPort( window );            
  325.                 BeginUpdate( window );
  326.                 drawWindow();
  327.                 EndUpdate( window );
  328.             }
  329.         }
  330.     }
  331. }